package com.chinacoal.hr.levelimpl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chinacoal.hr.levelapi.client.HolidayLawClient;
import com.chinacoal.hr.levelapi.client.response.Result;
import com.chinacoal.hr.levelapi.client.response.ResultGenerator;
import com.chinacoal.hr.levelapi.entity.HolidayLaws;
import com.chinacoal.hr.levelimpl.model.DayLawManager;
import com.chinacoal.hr.levelimpl.service.HolidayLawsService;
import com.chinacoal.hr.levelimpl.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author: YunTao.Li
 * @create: 2019-12-18 22:39
 * @description:
 **/
@RestController
public class HolidayLawController implements HolidayLawClient{

    @Autowired
    private HolidayLawsService holidayLawsService;

    /**
     * 查询假期排班信息列表
     *
     * @param page  :
     * @param limit :
     * @param year  :
     * @param hdate :
     * @return : com.chinacoal.hr.levelimpl.response.Result
     * @author : YunTao.Li
     * @date : 2019/12/18 2019/12/18
     */
    public Result list(long page,
                       long limit,
                       String year,
                       String hdate) {

        IPage<HolidayLaws> recordList = null;
        try {
            recordList = holidayLawsService.getHolidayLawsList(page, limit, year, hdate);
        } catch (Throwable e) {
            return ResultGenerator.genFailedResult(e.getMessage());
        }
        return ResultGenerator.genOkResult(recordList);
    }

    /**
     * 新增假期排班信息
     *
     * @param record :
     * @return : com.chinacoal.hr.levelimpl.response.Result
     * @author : YunTao.Li
     * @date : 2019/12/18 2019/12/18
     */
    public Result add( HolidayLaws record) {
        Map<String, String> result = null;
        try {
            result = holidayLawsService.insertHolidayLaw(record);
            if ("-1".equals(result.get("code"))) {
                String message = result.get("message");
                ResultGenerator.genFailedResult(message);
            }
        } catch (Throwable e) {
            e.printStackTrace();
            ResultGenerator.genFailedResult(e.getMessage());
        }
        return ResultGenerator.genOkResult();
    }

    /**
     * 编辑假期排班信息
     *
     * @param holidayId :
     * @param record    :
     * @return : com.chinacoal.hr.levelimpl.response.Result
     * @author : YunTao.Li
     * @date : 2019/12/18 2019/12/18
     */
    public Result update(String holidayId,
                         HolidayLaws record) {

        Map<String, String> result = null;

        try {
            result = holidayLawsService.updateHolidayLaw(record);

            if ("-1".equals(result.get("code"))) {
                String message = result.get("message");
                return ResultGenerator.genFailedResult(message);
            }
        } catch (Throwable e) {
            e.printStackTrace();
            return ResultGenerator.genFailedResult(e.getMessage());
        }

        return ResultGenerator.genOkResult();
    }

    /**
     * 删除假期排版信息
     *
     * @param holidayId :
     * @return : com.chinacoal.hr.levelimpl.response.Result
     * @author : YunTao.Li
     * @date : 2019/12/18 2019/12/18
     */
    public Result delete( String holidayId) {
        Map<String, String> result = null;

        try {
            result = holidayLawsService.deleteHolidayLaw(holidayId);
            if ("-1".equals(result.get("code"))) {
                String message = result.get("message");
                return ResultGenerator.genFailedResult(message);
            }
        } catch (Throwable e) {
            e.printStackTrace();
            return ResultGenerator.genFailedResult(e.getMessage());
        }

        return ResultGenerator.genOkResult();
    }

    /**
     * 时间计算
     *
     * @param startDate     :
     * @param startTimeType :
     * @param endDate       :
     * @param endTimeType   :
     * @return : com.chinacoal.hr.levelimpl.response.Result
     * @author : YunTao.Li
     * @date : 2019/12/27 2019/12/27
     */
    public Result getLawMgr( String startDate,
                             String startTimeType,
                             String endDate,
                             String endTimeType) {
        DayLawManager dayLawManager = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {

            dayLawManager = DateUtil.getDayLawManager(format.parse(startDate), startTimeType, format.parse(endDate), endTimeType);
        } catch (Throwable e) {
            e.printStackTrace();
            return ResultGenerator.genFailedResult(e.getMessage());
        }
        return ResultGenerator.genOkResult(dayLawManager);
    }

}
